package br.com.lucasddaniel.merchanmobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.lucasddaniel.merchanmobile.R;
import br.com.lucasddaniel.merchanmobile.bean.PacoteArquivoBean;

public class PacotesAdapter extends ArrayAdapter<PacoteArquivoBean> {
	private Context context = null;
	private List<PacoteArquivoBean> listPacotesBean = null;
	private List<PacoteArquivoBean> items;

	public List<PacoteArquivoBean> getListPacotesBean() {
		return listPacotesBean;
	}

	public void setListPacotesBean(List<PacoteArquivoBean> listPacotesBean) {
		this.listPacotesBean = listPacotesBean;
	}

	public List<PacoteArquivoBean> getItems() {
		return items;
	}

	public void setItems(List<PacoteArquivoBean> items) {
		this.items = items;
	}

	public int getCount() {

		return (items == null) ? 0 : items.size();
	}

	public PacoteArquivoBean getItem(int position) {

		return items.get(position);
	}

	private MyFilter filter;

	public PacotesAdapter(Context context, int resource, List<PacoteArquivoBean> listPacotesBean) {
		super(context, resource, listPacotesBean);
		this.context = context;
		this.listPacotesBean = listPacotesBean;
		this.items = listPacotesBean;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.row_pacotes, parent, false);
		if (items != null && items.size() != 0) {
			final PacoteArquivoBean pacotesBean = items.get(position);
			if (pacotesBean != null) {
				TextView txtViewNome = (TextView) rowView.findViewById(R.id_row_pacotes.txtview_nome);
				ImageView fotoPacote = (ImageView) rowView.findViewById(R.id_row_pacotes.foto_pacote);
				switch (pacotesBean.getId()) {
				case 1:
					fotoPacote.setImageResource(R.drawable.arraial_do_cabo);
					break;
				case 2:
					fotoPacote.setImageResource(R.drawable.rio_de_janeiro);
					break;
				case 3:
					fotoPacote.setImageResource(R.drawable.barco);
					break;
				case 4:
					fotoPacote.setImageResource(R.drawable.noite_de_samba);
					break;
				case 5:
					fotoPacote.setImageResource(R.drawable.city_tour);
					break;
				case 6:
					fotoPacote.setImageResource(R.drawable.transfer);
					break;
				default:
					break;
				}
				TextView txtViewPreco = (TextView) rowView.findViewById(R.id_row_pacotes.txtview_preco);
				TextView txtViewPrecoDolar = (TextView) rowView.findViewById(R.id_row_pacotes.txtview_preco_dolar);
				// final ImageView checkBox = ( ImageView )
				// rowView.findViewById( R.id_row_pacotes.checkbox );
				txtViewNome.setText(pacotesBean.getNome());
				txtViewPreco.setText("R$ " + pacotesBean.getPreco().toString());
				txtViewPrecoDolar.setText("  $ " + pacotesBean.getPrecoDolar().toString());
				// checkBox.setChecked(pacotesBean.isChecked());
				// checkBox.setOnClickListener(new OnClickListener() {
				//
				// public void onClick(View v) {
				// pacotesBean.setChecked(checkBox.isChecked());
				// }
				// });
			}
		}
		return rowView;
	}

	@Override
	public Filter getFilter() {
		if (this.filter == null)
			this.filter = new MyFilter(this);
		return this.filter;
	}
}

class MyFilter extends Filter {

	private PacotesAdapter adapter;

	public MyFilter(PacotesAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	protected FilterResults performFiltering(CharSequence constraint) {
		FilterResults results = new FilterResults();

		if (constraint.toString().equals("")) {
			results.values = adapter.getListPacotesBean();
			results.count = adapter.getListPacotesBean().size();
			return results;
		}
		List<PacoteArquivoBean> filteredItems = new ArrayList<PacoteArquivoBean>();
		for (PacoteArquivoBean o : adapter.getListPacotesBean()) {
			if (o.toString().toUpperCase().contains(constraint.toString().toUpperCase())) {
				filteredItems.add(o);
			}
		}

		results.values = filteredItems;
		results.count = filteredItems.size();
		return results;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void publishResults(CharSequence constraint, FilterResults results) {
		adapter.setItems((List<PacoteArquivoBean>) results.values);
	}

}
